/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jpAgregaDetalleLicitacion.java
 *
 * Created on 03-ago-2010, 15:38:38
 */

package Licitaciones;

import administracion.BeanBinding.TblBeanBindingDetalleLicitacion;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.VerificarDatos;
import com.jidesoft.hints.ListDataIntelliHints;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JDialog;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import suministros.ejb30.TO.SuministroTO;

/**
 *
 * @author Diana
 */
public class jpAgregaDetalleLicitacion extends javax.swing.JPanel {
    private ListDataIntelliHints intelliHintsSuministros;
    SuministroTO suministro;
    jifRegistrarLicitacion jpPadre;
    DefaultFormatterFactory currFactory;
    JDialog V_padre;
    /** Creates new form jpAgregaDetalleLicitacion */
    public jpAgregaDetalleLicitacion(JDialog jdPadre, jifRegistrarLicitacion padre) {
        this.jpPadre = padre;
        this.V_padre = jdPadre;
        construirMascaras();
        initComponents();
        limpiar();
        cargaHintsIndependientes();
    }
     private void limpiar() {
        this.jtfCodSuministro.setText("");
        this.jtfDescSuministro.setText("");
        this.jftfCantidad.setValue(new Double(0.0D));
        this.jftfPrecio.setValue(new Double(0.0D));
        this.jtfCodSuministro.requestFocus();

  }

  private void construirMascaras() {
    NumberFormat editFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);

    editFormat.setGroupingUsed(false);

    NumberFormatter enFormat = new NumberFormatter(editFormat);

    this.currFactory = new DefaultFormatterFactory(enFormat, enFormat, enFormat);

    enFormat.setAllowsInvalid(true);
  }
private void cargaHintsIndependientes() {
    this.intelliHintsSuministros = new ListDataIntelliHints(this.jtfDescSuministro, Listas.getSuministros());
  }
   private void buscarSuministros()
  {
    this.suministro = null;
    SuministroTO sumi;
    if (this.jtfCodSuministro.getText().trim().length() < 1) {

      try { sumi = (SuministroTO)this.intelliHintsSuministros.getSelectedHint();
      } catch (NullPointerException ex) {
        sumi = null;
      }
    } else {
      sumi = Listas.getSuministroCodigo(this.jtfCodSuministro.getText().trim());
    }

    if (sumi == null) {
      Utiles_MensajesDialogo.mensajeError("El suministro no existe!");
      this.jtfDescSuministro.setText("");
      this.jbAceptar.setEnabled(false);
     } else {
      this.jtfDescSuministro.setText(sumi.toString());
      this.jtfCodSuministro.setText(sumi.getId_suministro());
      this.jbAceptar.setEnabled(true);
      this.jftfCantidad.requestFocus();
      this.suministro = sumi;
    }
  }
   public void obtenerSumaMontos(){

     }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel12 = new javax.swing.JLabel();
        jftfCantidad = new javax.swing.JFormattedTextField();
        this.jftfCantidad.setFormatterFactory(this.currFactory);
        jLabel13 = new javax.swing.JLabel();
        jbCancelar = new javax.swing.JButton();
        jbAceptar = new javax.swing.JButton();
        jpInversion = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jtfCodSuministro = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jtfDescSuministro = new javax.swing.JTextField();
        jftfPrecio = new javax.swing.JFormattedTextField();
        this.jftfPrecio.setFormatterFactory(this.currFactory);

        jLabel12.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel12.setText("CANTIDAD");

        jftfCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jftfCantidadActionPerformed(evt);
            }
        });

        jLabel13.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel13.setText("PRECIO");

        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CERRAR");
        jbCancelar.setToolTipText("Cancelar la Operacion");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jbCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelarKeyPressed(evt);
            }
        });

        jbAceptar.setFont(new java.awt.Font("Arial", 0, 12));
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText("ACEPTAR");
        jbAceptar.setToolTipText("Registrar el pedido");
        jbAceptar.setEnabled(false);
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel9.setText("COD.SUMINISTRO");

        jtfCodSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12));
        jtfCodSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfCodSuministroActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel10.setText("DESC.SUMINISTRO");

        jtfDescSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12));
        jtfDescSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDescSuministroActionPerformed(evt);
            }
        });
        jtfDescSuministro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescSuministroKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jpInversionLayout = new javax.swing.GroupLayout(jpInversion);
        jpInversion.setLayout(jpInversionLayout);
        jpInversionLayout.setHorizontalGroup(
            jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpInversionLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(88, 88, 88))
        );
        jpInversionLayout.setVerticalGroup(
            jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel9)
                .addComponent(jLabel10)
                .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jftfPrecio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jftfPrecioActionPerformed(evt);
            }
        });
        jftfPrecio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jftfPrecioKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addGap(51, 51, 51)
                .addComponent(jftfCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel13)
                .addGap(62, 62, 62)
                .addComponent(jftfPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(172, 172, 172)
                .addComponent(jbAceptar)
                .addGap(18, 18, 18)
                .addComponent(jbCancelar))
            .addComponent(jpInversion, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jpInversion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jftfCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(jftfPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAceptar)
                    .addComponent(jbCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jftfCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jftfCantidadActionPerformed

        this.jftfPrecio.requestFocus();
}//GEN-LAST:event_jftfCantidadActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        
        this.V_padre.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelarKeyPressed
        
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbCancelar.doClick();
        }
}//GEN-LAST:event_jbCancelarKeyPressed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
       

        TblBeanBindingDetalleLicitacion detalle = new TblBeanBindingDetalleLicitacion();
        try {
            float cantidad = VerificarDatos.verificaFloatSoloPositivo(this.jftfCantidad.getValue());
            if(cantidad==0 ){
                Utiles_MensajesDialogo.mensajeError("No se pudo guardar el detalle\n" + "La cantidad debe ser mayor a 0");
                return;
            }
            float precio = VerificarDatos.verificaFloatSoloPositivo(this.jftfPrecio.getValue());
            if(precio==0 ){
                Utiles_MensajesDialogo.mensajeError("No se pudo guardar el detalle\n" + "El precio debe ser mayor a 0");
                return;
            }
            detalle.setId_suministro(this.suministro.getId_suministro());
            detalle.setDesc_suministro(this.suministro.getDescripcion());
            detalle.setCantidad(new BigDecimal(cantidad).setScale(2, RoundingMode.HALF_UP));
            detalle.setPrecio(new BigDecimal(precio).setScale(2,RoundingMode.HALF_UP));
            detalle.setPrecioTotal(detalle.getCantidad().multiply(detalle.getPrecio()));
            detalle.setMigracion("");
            BigDecimal suma=BigDecimal.ZERO;
            //verificar montos no sobrepasan
             for(int i=0; i<jpPadre.detalleLicitacion.size(); i++){
                BigDecimal monto=jpPadre.detalleLicitacion.get(i).getCantidad().multiply(jpPadre.detalleLicitacion.get(i).getPrecio());
                suma=suma.add(monto);
             }
             suma=suma.add(detalle.getPrecioTotal());
             if((new BigDecimal(this.jpPadre.jtfMonto.getText())).compareTo(suma)<0){
                 Utiles_MensajesDialogo.mensajeAlerta("El monto total sobrepasa al disponible en la licitacion");
             }
             else{
                this.jpPadre.agregaDetalleSuministro(detalle);
                jpPadre.actualizaTablaDetalle();
                limpiar();
             }
                  
        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeError("No se pudo guardar el detalle\n" + e.getMessage());
        }
}//GEN-LAST:event_jbAceptarActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.doClick();
        }
}//GEN-LAST:event_jbAceptarKeyPressed

    private void jtfCodSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfCodSuministroActionPerformed
       
        if (this.jtfCodSuministro.getText().trim().length() < 1)
            this.jtfDescSuministro.requestFocus();
        else
            buscarSuministros();
}//GEN-LAST:event_jtfCodSuministroActionPerformed

    private void jtfDescSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDescSuministroActionPerformed
       
        buscarSuministros();
}//GEN-LAST:event_jtfDescSuministroActionPerformed

    private void jtfDescSuministroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescSuministroKeyPressed
        
        if (this.jtfCodSuministro.getText().trim().length() > 0)
            this.jtfCodSuministro.setText("");
}//GEN-LAST:event_jtfDescSuministroKeyPressed

    private void jftfPrecioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jftfPrecioActionPerformed
       
    }//GEN-LAST:event_jftfPrecioActionPerformed

    private void jftfPrecioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jftfPrecioKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.requestFocus();
        }
    }//GEN-LAST:event_jftfPrecioKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JFormattedTextField jftfCantidad;
    private javax.swing.JFormattedTextField jftfPrecio;
    private javax.swing.JPanel jpInversion;
    private javax.swing.JTextField jtfCodSuministro;
    private javax.swing.JTextField jtfDescSuministro;
    // End of variables declaration//GEN-END:variables

}
